package com.zbl.zblproject.member.controller;

import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.member.entity.Member;
import com.zbl.zblproject.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2019/3/27
 */
@RestController
@Api(tags = "会员接口")
@RequestMapping("/member")
public class MemberController {






    @Autowired
    private MemberService memberService;

    @GetMapping("/search")
    @ApiOperation("查询用户")
    public Result searchUser(@ApiParam("用户OpenId") @RequestParam("openId") String openId){
        Member member = memberService.searchMember(openId);
        if(member == null){
            return ResultUtil.error("没有这个用户!");
        }
        return ResultUtil.success("成功!", member);
    }


    @GetMapping("/integral/getUserIntegral/{telephone}")
    @ApiOperation("查询用户积分")
    public Result getUserIntegral(@ApiParam("用户手机号") @PathVariable("telephone") String telephone){
        Member member = memberService.searchByTelephone(telephone);
        if(member == null){
            return ResultUtil.error("no_user",null);
        }
        return ResultUtil.success("success", member);
    }



    @PostMapping("/add")
    @ApiOperation("添加用户的OpenId")
    public Result addUser(@ApiParam("openId") @RequestParam("openId") String openId,
                          @ApiParam("手机号码") @RequestParam("telephone") String telephone){
        Member member = new Member();
        member.setOpenId(openId);
        member.setTelephone(telephone);
        if(memberService.addMember(member)){
            return ResultUtil.success("添加成功！");
        }
        return ResultUtil.error("添加失败!");
    }

}
